import { Component, OnInit } from '@angular/core';
import {AccountService} from '../account.service';
import {NzMessageService} from 'ng-zorro-antd';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss'],
  providers: [AccountService]
})
export class UserComponent implements OnInit {
  merchant_info;
  point;
  canTopUp = true;
  _isSpinning = false;
  _isSpinning_userInfo = false;

  constructor(private accountService: AccountService,
              private _message: NzMessageService,
              private router: Router,
              private route: ActivatedRoute,
              ) { }

  ngOnInit() {
    this.getAccountDetail();
    this.getUserInfo();
  }

  getAccountDetail() {
    this._isSpinning = true;
    this.accountService.getAccountDetail().subscribe(res => {
      this._isSpinning = false;
      if (res.code === 2000) {
        this.point = res.data.point;
      } else if (res.code === 4040 && res.message === '未开通推客!') {
        this.point = 0;
        this.canTopUp = false;
      } else {
        this._message.error(res.message);
      }
    });
  }

  getUserInfo() {
    this._isSpinning_userInfo = true;
    this.accountService.getUserInfo().subscribe(res => {
      this._isSpinning_userInfo = false;
      if (res.code === 2000) {
        this.merchant_info = res.data;
      } else {
        this._message.error(res.message);
      }
    });
  }

  toTopUpMoney() {
    if (this.canTopUp) {
      this.router.navigate(['member/account/user/top-up-money']);
    } else {
      this._message.error('请先开通推客功能！');
    }
  }
}
